#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 19-6-27 上午11:31
# @Author  : Tom
# @Site    : 
# @File    : permission.py
# @Software: PyCharm
from django.contrib.contenttypes.models import ContentType
from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from django.contrib.auth.models import Permission, Group, PermissionsMixin
from meiduo_admin.serilizers import PermissionPersSerilizer, Permssioncontent_typesSerilizer, PermissionAdminSerilizer
from meiduo_admin.utils import PageNumber_User
from users.models import User


# 权限
class PermissionPers(ModelViewSet):
    serializer_class = PermissionPersSerilizer
    queryset = Permission.objects.order_by("pk")
    permission_classes = [IsAdminUser, ]
    pagination_class = PageNumber_User

    def content_types(self, request):
        group = Group.objects.all()
        ser = Permssioncontent_typesSerilizer(group, many=True)
        return Response(ser.data, status=201)


# 用户
class PermissionGroup(ModelViewSet):
    serializer_class = Permssioncontent_typesSerilizer
    queryset = Group.objects.all()
    permission_classes = [IsAdminUser, ]
    pagination_class = PageNumber_User

    def permissionsimple(self, request):
        pers = Permission.objects.all()
        res = PermissionPersSerilizer(pers, many=True)
        return Response(res.data, status=201)


# 管理员
class PermissionAdmins(ModelViewSet):
    serializer_class = PermissionAdminSerilizer
    queryset = User.objects.filter(is_staff=True)
    permission_classes = [IsAdminUser, ]
    pagination_class = PageNumber_User

    # PermissionsMixin.objects.create(groups_id=76, user_permissions_id=23)
    # PermissionsMixin.UserManager.get(id=2)

    def permissiongroupsimple(self, request):
        group = Group.objects.all()
        res = Permssioncontent_typesSerilizer(group, many=True)
        return Response(res.data)

        # def retrieve(self, request, *args, **kwargs):
        #     response_dict = dict()
        #     user = User.objects.get(pk=kwargs['pk'])
        #     response_dict["username"] = user.username
        #     response_dict["password"] = user.password
        #     response_dict["mobile"] = user.mobile
        #     response_dict["email"] = user.email
        #     permission = Permission.objects.get(user=user)
        #     group = Group.objects.get(user=user)
        #     response_dict["groups"] = [group.id]
        #     response_dict["user_permissions"] = [permission.id]
        #     return Response(response_dict)
